<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>
    <!-- table -->

    <FormPanel ref="formPanel1" :list="formList" :cols="1" :formType="'edit'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div style="padding-top: 1rem;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
      <multiuse-button @click="closeDialog">关 闭</multiuse-button>
    </div>
    <BaseDialog :visible.sync="ModelVisible" :title="'车型选择'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <ModelVehicle @close="modelClose" @save="modelSave"></ModelVehicle>
    </BaseDialog>

    </Spin>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';//字典的引入
import { editSave } from '@/api/claim/factory/guaranteeOil';
import ModelVehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
import { selectById } from '@/api/claim/factory/guaranteeOil';

export default {
   mixins: [dictionary],
   props: ['type', 'rowData'],
  components: {
    ModelVehicle,

  },
  data() {
    return {
      loading: false,
      ModelVisible: false,
      collapse: false,
      infoId: null,
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList(),
      saveData: null,
      editData: null
    };
  },
  created() {},
  mounted() {
    this.init();
  },
  methods: {
    async multiple(val) {
      this.formList.find((x) => x.fieldName == 'mngLab').initialValue = parseFloat((val/ this.para1)*100).toFixed(2);
      // const res = await multiples({ A: this.para1, B: val });
      // if (res.resultCode == 200) {
      //   this.formList.find((x) => x.fieldName == 'labPrice').initialValue = res.data;
      // }
    },
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    async init() {
      this.loading = true;
      let newVar = await selectById({id: this.rowData.guaranteeId});
      this.editData = newVar.data;
      this.formList.forEach(x => {
        x.initialValue = newVar.data[x.fieldName];
      });
      this.loading = false;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
      console.log(val);
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //保存
    async saveDialog() {
      this.$refs.formPanel1.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.saveData.createdBy = this.editData.createdBy;
        this.saveData.createdAt = this.editData.createdAt;
        this.saveData.guaranteeId= this.editData.guaranteeId;
        this.saveData.isDeleted=this.editData.isDeleted;
        this.saveData.appId=this.editData.appId;
        this.saveData.modelId = this.modelId;
        this.loading = true;

        let res = await editSave(this.saveData);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({ title: '提示信息', message: '保存成功' });
        } else {
          this.loading = false;
          // this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
          // this.closeDialog();
        }
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', {visible: false, data: this.selectedRows, type: 'close'});
    },
    modelTurnOn() {
      this.ModelVisible = true;
    },

    modelClose(){
      this.ModelVisible = false;
    },

    modelSave(obj) {
      this.formList.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
      this.formList.find(x => x.fieldName == 'modelName').initialValue = obj[0].modelName;
      this.modelId = obj[0].modelId;
      this.ModelVisible = false;
    },

    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '车型编码',
          fieldName: 'modelCode',
          placeholder: '请选择车型编码...',
          readonly: true,// 只读
          rules: [{required: true, message: '请选择车型编码...', trigger: 'change'}],
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.modelTurnOn()}/>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '车型名称',
          fieldName: 'modelName',
          readonly: true,// 只读

          placeholder: '请选择车型名称...',
          rules: [{required: true, message: '请输入车型名称...', trigger: 'change'}],
        },
        {
            type: 'SELECT',
            label: '类型',
            fieldName: 'type',
            itemList: [],
            placeholder: '请输入类型...',
            filterable: true,
            rules: [{ required: true, message: '请选择类型', trigger: 'change' }],
            itemList: this.createDictList('5593'),
            editRequired: true
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'ptStatus',
          placeholder: '请选择状态...',
          initialValue: '',
          filterable: true,
          itemList: [{text: '启用', value: 1}, {text: '禁用', value: 0}],
          rules: [{required: true, message: '请选择状态...', trigger: 'change'}],
        },
          {
          type: 'INPUT',
          label: '工时单价',
          fieldName: 'labPrice',
          placeholder: '请输入工时单价',
          // disabled: true,
          unitRender: (p) => {
            return <div>元</div>;
          },
          rules: [
            { required: true, message: '请输入工时单价', trigger: 'change' },
            {
              validator: (rule, value, callback) => {
                let reg = /^(\d+)(\.?)(\d{0,2})$/;
                if (value < 0) {
                  return callback(new Error('工时单价,最小为0'));
                }
                if(!reg.test(Number(value))){
                  return callback(new Error('工时单价,最多2位小数'));
                }
                callback();
              },
              trigger: 'change',
            }
          ],
          change: (val) => {
            this.multiple(val);
          },
        },
          {
            type: 'TEXT_AREA',
            label: '备注',
            fieldName: 'remarks',
            placeholder: '请输入备注...',
            rows: 3,
            selfCols: 1 // 元素占据几行
          }


      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
